package com.glas.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.glas.entity.UserEntity;
import com.glas.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;


    /**
     * http://127.0.0.1:8080/user/list?name=&age=
     * @param name
     * @param age
     * @return
     */
    @RequestMapping("/list")
    public Object getUserList(String name, Integer age){

        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<UserEntity>().like("name", name);
        if(age != null){
            queryWrapper.eq("age", age);
        }
        List<UserEntity> list = userService.list(queryWrapper);
        log.info(list.toString());

        return list;
    }

    /**
     * 增加人员
     * http://127.0.0.1:8080/user/addUser?name=博尔特&age=22&sex=1
     * @param userEntity
     * @return
     */
    @RequestMapping("/addUser")
    public Object addUser(UserEntity userEntity){
        userService.save(userEntity);
        return "success";
    }

    /**
     * 修改人员
     * http://127.0.0.1:8080/user/update?id=3&name=黑莓&age=22&sex=0
     * @return
     */
    @RequestMapping("/update")
    public Object updateUser(UserEntity userEntity){
        System.out.println(userEntity);

        userService.updateById(userEntity);

        return "success";
    }


    /**
     * 删除人员
     * http://127.0.0.1:8080/user/del?id=6
     * @param userEntity
     * @return
     */
    @RequestMapping("/del")
    public Object deleteUser(UserEntity userEntity){
        userService.removeById(userEntity.getId());
        return "success";
    }



}
